<?php
namespace app\admin\controller;

use app\common\controller\AdminBase;
use think\Db;
use think\Session;

/**
* 
*/
class Reward extends AdminBase
{
	
	public function invite(){
		if( request()->isAjax() ){

            $map = input("param.");
            $where = "";
            if( empty($map["type"]) ){
                $where = " a.reason_id = 1 ";
            }else{
                $where = " a.reason_id = " . $map["type"];
            }

            if(!empty($map["user_id"])){
                $where .= " AND a.user_id = '".$map["user_id"]."'";
            }
            if( !empty($map["start_time"]) ){

                $where .= " AND a.c_time >= ".strtotime($map["start_time"]);
            }
            if( !empty($map["end_time"]) ){
                if( empty($map["start_time"]) ){
                    $time = time() - (60*60*24*7);
                    $where .= " AND a.c_time >= ".$time;
                }
                $where .= " AND a.c_time <= ".strtotime($map["end_time"]);
            }
            //print_r($map);exit;
            $count = Db::name("account_log")->alias("a")
                //->join("ysy_user b","a.user_id = b.id","LEFT")
                ->where($where)->count();
            
            $limitForm = ( $map["page"] -1 )* $map["limit"];

            $list = Db::name("account_log")->alias("a")
                ->join("ysy_user b","a.user_id = b.id","LEFT")
                ->join("ysy_user_info c","c.user_id = a.user_id","LEFT")
                ->join("ysy_level d","d.id = b.level_id","LEFT")
                ->field("a.*,b.id as user_id,b.nickname,d.level")
                ->where($where)
                ->order("a.id desc")
                ->limit($limitForm,$map["limit"])
                ->select();
            
            $data = [];
            foreach($list as $k=>$v){
                $data[$k] = $v;
                $data[$k]["levelName"] = $v["level"];
                $data[$k]["c_time"] = date('y-m-d H:i:s', $v["add_time"]);
                //$data[$k]["typeName"] = $v["type"] == 5 ? "静态" : "动态";
            }
            //print_r($data);

            $retArr = [
                "code"=>0,
                "msg" => "",
                "count" => $count,
                "data" => $data
            ];
            echo json_encode($retArr);
            exit();
        }
        $map = input('param.');
        $url = '/admin/Reward/invite';
        $Arr = [];
        if( empty($map["type"]) ){
            $url.="?type=1";
            $Arr["type"] = 1;
        }else{
            $url.="?type=".$map["type"];
            $Arr["type"] = $map["type"];
        }
        if( !empty($map["user_id"]) ){
            $url.="&user_id=".$map["user_id"];
            $Arr["user_id"] = $map["user_id"];
        }

        if( !empty($map["start_time"]) ){
            $url.="&start_time=".$map["start_time"];
            $Arr["start_time"] = $map["start_time"];
        }
        if( !empty($map["end_time"]) ){
            $url.="&end_time=".$map["end_time"];
            $Arr["end_time"] = $map["end_time"];
        }
        $this->assign("Arr",$Arr);
        $this->assign("url",$url);
        return $this->fetch();
	}
}